package com.yc.snack.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.snack.biz.ITypeInfoBiz;
import com.yc.snack.biz.impl.TypeInfoBizImpl;
import com.yc.snack.entity.TypeInfo;
import com.yc.snack.util.RequestParamUtil;

@WebServlet("/type/*")
public class TypeInfoController extends BaseController {
	//序列化起始就是对象输入流和对象输出流
	
	private static final long serialVersionUID = 9074396926163284627L;
	
	/**
	 * 添加类型的方法
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void add(HttpServletRequest request,HttpServletResponse response)throws IOException {
		TypeInfo tf = RequestParamUtil.getParams(TypeInfo.class,request);
		ITypeInfoBiz  typeInfoBiz = new TypeInfoBizImpl();
		int result = typeInfoBiz.add(tf);
		if(result > 0) {
			this.send(response, 200,"成功");
			return;
		}
		this.send(response, 500,"失败");
	}
	
	public void finds(HttpServletRequest request,HttpServletResponse response)throws IOException {
		//默认情况下，layui分页发送的是page(查第几页) 和 limit(每页查多少条)
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("limit"));

		ITypeInfoBiz  typeInfoBiz = new TypeInfoBizImpl();
		Map<String, Object> map = typeInfoBiz.findByPage(page, rows);
		this.send(response, map);
		return;
	}
	
	public void findAll(HttpServletRequest request,HttpServletResponse response)throws IOException {
		ITypeInfoBiz  typeInfoBiz = new TypeInfoBizImpl();
		List<TypeInfo> list = typeInfoBiz.finds();
		if(list == null || list.isEmpty()) {
			this.send(response, 600,"暂无数据");
			return;
		}
		this.send(response, 200,list);
		return;
	}
}